﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace Quebec.Phone.Utilities
{
	public static class StorageHandler<T>
	{
		/// <summary>
		/// Add value to ApplicationSettings. If key already exists
		/// it will be deleted and added again.
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		public static void StoreToSetting(string key, T obj)
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

			if (settings.Contains(key))
				settings.Remove(key);

			settings.Add(key, obj);
			settings.Save();
		}

		/// <summary>
		/// Read value from ApplicationSettings. If the key is not 
		/// found null is returned.
		/// </summary>
		/// <param name="key"></param>
		/// <returns></returns>
		public static T RetrieveFromSetting(string key)
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

			if (settings.Contains(key))
			{
				return (T)settings[key];
			}

			return default(T);
		}
	}
}
